package org.locker.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.locker.domain.bo.WechatMpInfoBo;
import org.locker.domain.vo.WechatMpInfoVo;
import org.locker.service.IWechatMpInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.locker.common.idempotent.annotation.RepeatSubmit;
import org.locker.common.log.annotation.Log;
import org.locker.common.web.core.BaseController;
import org.locker.common.mybatis.core.page.PageQuery;
import org.locker.common.core.domain.R;
import org.locker.common.core.validate.AddGroup;
import org.locker.common.core.validate.EditGroup;
import org.locker.common.log.enums.BusinessType;
import org.locker.common.excel.utils.ExcelUtil;
import org.locker.common.mybatis.core.page.TableDataInfo;

/**
 * 系统参数
 *
 * @author winnie
 * @date 2024-05-21
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/device/wechatMpInfo")
public class WechatMpInfoController extends BaseController {

    private final IWechatMpInfoService wechatMpInfoService;
    @Autowired
    private WxMpService wxMpService;
    /**
     * 查询系统参数列表
     */
    @SaCheckPermission("device:wechatMpInfo:list")
    @GetMapping("/list")
    public TableDataInfo<WechatMpInfoVo> list(WechatMpInfoBo bo, PageQuery pageQuery) {
        return wechatMpInfoService.queryPageList(bo, pageQuery);
    }



    /**
     * 导出系统参数列表
     */
    @SaCheckPermission("device:wechatMpInfo:export")
    @Log(title = "系统参数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WechatMpInfoBo bo, HttpServletResponse response) {
        List<WechatMpInfoVo> list = wechatMpInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统参数", WechatMpInfoVo.class, response);
    }

    /**
     * 获取系统参数详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("device:wechatMpInfo:query")
    @GetMapping("/{id}")
    public R<WechatMpInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(wechatMpInfoService.queryById(id));
    }

    /**
     * 新增系统参数
     */
    @SaCheckPermission("device:wechatMpInfo:add")
    @Log(title = "系统参数", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WechatMpInfoBo bo) {
        bo.setStatus(1l);
        WxMpDefaultConfigImpl configStorage= new WxMpDefaultConfigImpl();
        configStorage.setAppId(bo.getAppId());
        configStorage.setSecret(bo.getAppSecret());
        configStorage.setToken(bo.getToken());
        configStorage.setAesKey(bo.getEncodingAesKey());
        wxMpService.addConfigStorage(bo.getAppId(),configStorage);
        return toAjax(wechatMpInfoService.insertByBo(bo));
    }

    /**
     * 修改系统参数
     */
    @SaCheckPermission("device:wechatMpInfo:edit")
    @Log(title = "系统参数", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WechatMpInfoBo bo) {
        WxMpDefaultConfigImpl configStorage= new WxMpDefaultConfigImpl();
        configStorage.setAppId(bo.getAppId());
        configStorage.setSecret(bo.getAppSecret());
        configStorage.setToken(bo.getToken());
        configStorage.setAesKey(bo.getEncodingAesKey());
        wxMpService.addConfigStorage(bo.getAppId(),configStorage);
        return toAjax(wechatMpInfoService.updateByBo(bo));
    }

    /**
     * 删除系统参数
     *
     * @param ids 主键串
     */
    @SaCheckPermission("device:wechatMpInfo:remove")
    @Log(title = "系统参数", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(wechatMpInfoService.deleteWithValidByIds(List.of(ids), true));
    }
}
